package com.cn.kehong.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cn.kehong.api.*;
import com.cn.kehong.domain.*;
import com.cn.kehong.utils.CommonUtil;
import com.cn.kehong.utils.JsonResponse;
import com.cn.kehong.utils.QueryForm;
import com.google.common.collect.Lists;

import java.net.URLDecoder;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/dateBackTo")
public class DateBackToController {

    @Autowired
    private IProductPeiTouService productPeiTouService;

    @Autowired
    private IProductThinPeriodService productThinPeriodService;
    @Autowired
    private IProductThickPeriodService productThickPeriodService;
    @Autowired
    private IProductChuciPeriodService productChuciPeriodService;

    @Autowired
    private IProductGzPeriodService productGzPeriodService;

    @Autowired
    private IProductSaojiePeriodService productSaojiePeriodService;
    @Autowired
    private IProductDianciPeriodService productDianciPeriodService;


    //制程追溯/起批配料-投料混料
    @RequestMapping("/getProductPeiTouBackList/{frontToken}")
    @ResponseBody
    public JsonResponse<List<ProductPeiTou>> getProductPeiTouBackList(QueryForm queryForm, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<List<ProductPeiTou> > response = new JsonResponse<List<ProductPeiTou> >();
        List<ProductPeiTou> lists = productPeiTouService.getProductPeiTouBackList(queryForm);
        response.setResult(lists);
        response.setSuccess(true);
        return  response;
    }
    //制程追溯/粗磨
    @RequestMapping("/getProductThinPeriodBackList/{frontToken}")
    @ResponseBody
    public JsonResponse<List<ProductThinPeriod>> getProductThinPeriodBackList(QueryForm queryForm, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<List<ProductThinPeriod> > response = new JsonResponse<List<ProductThinPeriod> >();
        List<ProductThinPeriod> lists = productThinPeriodService.getProductThinPeriodBackList(queryForm);
        response.setResult(lists);
        response.setSuccess(true);
        return  response;
    }
    //制程追溯/细磨
    @RequestMapping("/getProductThickPeriodBackList/{frontToken}")
    @ResponseBody
    public JsonResponse<List<ProductThickPeriod>> getProductThickPeriodBackList(QueryForm queryForm, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<List<ProductThickPeriod> > response = new JsonResponse<List<ProductThickPeriod> >();
        List<ProductThickPeriod> lists = productThickPeriodService.getProductThickPeriodBackList(queryForm);
        response.setResult(lists);
        response.setSuccess(true);
        return  response;
    }

    //制程追溯/除磁
    @RequestMapping("/getProductChuciPeriodBackList/{frontToken}")
    @ResponseBody
    public JsonResponse<List<ProductChuciPeriod>> getProductChuciPeriodBackList(QueryForm queryForm, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<List<ProductChuciPeriod> > response = new JsonResponse<List<ProductChuciPeriod> >();
        List<ProductChuciPeriod> lists = productChuciPeriodService.getProductChuciPeriodBackList(queryForm);
        response.setResult(lists);
        response.setSuccess(true);
        return  response;
    }



    //制程追溯/干燥
    @RequestMapping("/getProductGzPeriodBackList/{frontToken}")
    @ResponseBody
    public JsonResponse<List<ProductGzPeriod>> getProductGzPeriodBackList(QueryForm queryForm, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<List<ProductGzPeriod> > response = new JsonResponse<List<ProductGzPeriod> >();
        List<ProductGzPeriod> lists = productGzPeriodService.getProductGzPeriodBackList(queryForm);
        response.setResult(lists);
        response.setSuccess(true);
        return  response;
    }

    //制程追溯/烧结
    @RequestMapping("/getProductSaojieJobBackList/{frontToken}")
    @ResponseBody
    public JsonResponse<List<ProductSaojieJob>> getProductSaojieJobBackList(QueryForm queryForm, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<List<ProductSaojieJob> > response = new JsonResponse<List<ProductSaojieJob> >();
        List<ProductSaojieJob> lists = productSaojiePeriodService.getProductSaojieJobBackList(queryForm);
        response.setResult(lists);
        response.setSuccess(true);
        return  response;
    }

    //制程追溯/电除磁-分级
    @RequestMapping("/getProductDianciPeriodBackList/{frontToken}")
    @ResponseBody
    public JsonResponse<List<ProductDianciPeriod>> getProductDianciPeriodBackList(QueryForm queryForm, @PathVariable(value = "frontToken", required = true) String frontToken) throws Exception {
        JsonResponse<List<ProductDianciPeriod>> response = new JsonResponse<List<ProductDianciPeriod>>();
        List<ProductDianciPeriod> lists = productDianciPeriodService.getProductDianciPeriodBackList(queryForm);
        response.setResult(lists);
        response.setSuccess(true);
        return response;
    }

    //当前在产数量查询
    @RequestMapping("/getProductionCount/{frontToken}")
    @ResponseBody
    public JsonResponse<Map<String,Object>> getProductionCount( @PathVariable(value = "frontToken", required = true) String frontToken) throws Exception {
        JsonResponse<Map<String,Object>> response = new JsonResponse<Map<String,Object>>();
        Map<String,Object>  productionCount = productPeiTouService.getProductionCount();
        response.setResult(productionCount);
        response.setSuccess(true);
        return response;
    }


    //制程追溯数量查询
    @RequestMapping("/getProductBackCount/{frontToken}")
    @ResponseBody
    public JsonResponse<Map<String,Object>> getProductBackCount( @PathVariable(value = "frontToken", required = true) String frontToken) throws Exception {
        JsonResponse<Map<String,Object>> response = new JsonResponse<Map<String,Object>>();
        Map<String,Object>  producBackCount = productPeiTouService.getProductBackCount();
        response.setResult(producBackCount);
        response.setSuccess(true);
        return response;
    }

    @ResponseBody
    @RequestMapping("/exportDateBack")
    public void exportDateBack(String queryStr, HttpServletResponse response) throws Exception {
        QueryForm queryForm = new QueryForm();
        if(StringUtils.isNotEmpty(queryStr)){
            queryStr = URLDecoder.decode(queryStr,"utf-8");
            queryStr = new String(queryStr.getBytes("ISO-8859-1"),"utf-8");
            JSONObject jsonObject = JSON.parseObject(queryStr);
            queryForm.setType(jsonObject.getString("type"));
            queryForm.setFactoryName(jsonObject.getString("factoryName"));
            queryForm.setGradeValue(jsonObject.getString("gradeValue"));
            queryForm.setProductCode(jsonObject.getString("productCode"));
            queryForm.setChuciMachine(jsonObject.getString("chuciMachine"));
            queryForm.setBoxCode(jsonObject.getString("boxCode"));
            queryForm.setThickGuanCode(jsonObject.getString("thinGuanCode"));
            queryForm.setResverGuan(jsonObject.getString("resverGuan"));
            queryForm.setThinFinalGuan(jsonObject.getString("thinFinalGuan"));
            queryForm.setXimoLine(jsonObject.getString("ximoLine"));
            queryForm.setThickGuanCode(jsonObject.getString("thickGuanCode"));
            queryForm.setChengGuanCode(jsonObject.getString("chengGuanCode"));
            queryForm.setGzGuanCode(jsonObject.getString("gzGuanCode"));
            queryForm.setYaoGuanCode(jsonObject.getString("yaoGuanCode"));
            queryForm.setDianChuciCode(jsonObject.getString("dianChuciCode"));
            queryForm.setFenjiCode(jsonObject.getString("fenjiCode"));
            queryForm.setStartTime(jsonObject.getString("startTime"));
            queryForm.setEndTime(jsonObject.getString("endTime"));
            queryForm.setPeiStatus(jsonObject.getString("peiStatus"));
            queryForm.setTouStatus(jsonObject.getString("touStatus"));
            queryForm.setStatus(jsonObject.getString("status"));
        }
        productPeiTouService.exportDateBack(queryForm,response);
    }
}
